﻿using M5.GeminiHybird.Core.Attribute;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace M5.GeminiHybird.Core.JavascriptEvent
{
    public class WinFormEvent : BaseJavescriptEvent
    {
        /// <summary>
        /// 打开窗体
        /// </summary>
        /// <param name="openFormName">需要打开窗体的Name值, 需要区分大小写</param>
        [JavascriptMethod]
        public void Open(string openFormName)
        {
            if (string.IsNullOrEmpty(openFormName))
            {
                throw new ArgumentException("openFormName不能为空");
            }

            string openFormNameLower = openFormName.ToLower();
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().Name.ToLower() == openFormNameLower)
                {
                    form.Show();
                    return;
                }
            }


            Assembly currEXEAssembly = Assembly.GetEntryAssembly();
            string exeName = currEXEAssembly.GetName().Name;
            Form openForm = currEXEAssembly.CreateInstance($"{exeName}.{openFormName}") as Form;
            if (openForm != null)
            {
                openForm.Show();
            }
        }

        [JavascriptMethod]
        public void Exit()
        {
            CurrExcuteFrom.Hide();
            Environment.Exit(0);
        }

        [JavascriptMethod]
        public void Close(string formName)
        {
            if (string.IsNullOrEmpty(formName))
            {
                CurrExcuteFrom.Close();
                return;
            }

            formName = formName.ToLower();
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().Name.ToLower() == formName)
                {
                    form.Close();
                    return;
                }
            }
        }

        [JavascriptMethod]
        public void Hide(string formName)
        {
            if (string.IsNullOrEmpty(formName))
            {
                CurrExcuteFrom.Hide();
                return;
            }

            formName = formName.ToLower();
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().Name.ToLower() == formName)
                {
                    form.Hide();
                    return;
                }
            }
        }

        /// <summary>
        /// 改变窗体大小
        /// </summary>
        /// <param name="formName">需要改变窗体的Name值, 不区分大小写</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        [JavascriptMethod]
        public void Resize(string formName, int width, int height)
        {
            if (string.IsNullOrEmpty(formName))
            {
                CurrExcuteFrom.Width = width;
                CurrExcuteFrom.Height = height;
            }

            formName = formName.ToLower();
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().Name.ToLower() == formName)
                {
                    form.Width = width;
                    form.Height = height;
                    return;
                }
            }
        }


        private static Point BeginMousePoint { get; set; } = Point.Empty;
        private static bool BeginDrag { get; set; } = false;

        [JavascriptMethod]
        public void Drag_MouseDown(int x, int y)
        {
            BeginDrag = true;
            BeginMousePoint = new Point(x, y);

        }
        [JavascriptMethod]
        public void Drag_MouseMove(int x, int y)
        {
            if (BeginDrag)
            {
                int moveX = x - BeginMousePoint.X;
                int moveY = y - BeginMousePoint.Y;
                CurrExcuteFrom.Location = new Point(CurrExcuteFrom.Location.X + moveX, CurrExcuteFrom.Location.Y + moveY);
            }
        }
        [JavascriptMethod]
        public void Drag_MouseUp()
        {
            BeginDrag = false;
            BeginMousePoint = Point.Empty;
        }

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <returns>文件绝对路径</returns>
        [JavascriptMethod]
        public string ChooseFile(string extensionFilter)
        {
            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();

            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);

                if (!string.IsNullOrEmpty(extensionFilter))
                {
                    string[] filter = extensionFilter.Split(',');
                    if (!filter.ToList().Contains(extension))
                    {
                        MessageBox.Show($"暂不支持{extension}的文件");
                        return "";
                    }
                }

                
                //声明允许的后缀名
                //string[] str = new string[] { ".gif", ".jpge", ".jpg" };
                //if (!((System.Collections.IList)str).Contains(extension))
                //{
                //    MessageBox.Show("仅能上传gif,jpge,jpg格式的图片！");
                //}
                //else
                //{
                    return fileDialog.FileName;
                //}
            }
            return "";
        }


        [JavascriptMethod]
        public object Location()
        {
            return new
            {
                x = CurrExcuteFrom.Location.X,
                y = CurrExcuteFrom.Location.Y
            };
        }

        [JavascriptMethod]
        public object Size()
        {
            return new
            {
                width = CurrExcuteFrom.Width,
                height = CurrExcuteFrom.Height
            };
        }
    }
}
